package com.mingmen.canting.caigourenyuan;

import android.content.Context;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.mingmen.canting.R;
import com.mingmen.canting.adapter.BohuiListAdapter;
import com.mingmen.canting.jingli.JingLiRuRightAdapter;
import com.mingmen.canting.model.CaiWanModel;
import com.mingmen.canting.model.JingLiRukuModel;
import com.mingmen.canting.model.SimpleData;
import com.mingmen.canting.model.ZhenBanShenHe;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * Created by yjwfn on 17-9-23.
 */

public class CaiGouWanAdapter extends RecyclerView.Adapter<CaiGouWanAdapter.PhotoViewHolder> {


    Context context;
    List<CaiWanModel.DataBean> dataBeanList;
    List<CaiWanModel.DataBean.DetailsBean> second = new ArrayList<>();
    private OnItemClickListener mOnItemClickListener;

    public CaiGouWanAdapter(Context context, List<CaiWanModel.DataBean> dataBeanList) {
        this.context = context;
        this.dataBeanList = dataBeanList;
    }

    public void setDataList(List<CaiWanModel.DataBean> dataBeanList) {
        this.dataBeanList = dataBeanList;

        notifyDataSetChanged();

    }

    public interface OnItemClickListener {
        //        void onItemClickListener(int pos, DaishenheListAdapter.ViewName viewName, List<MyLiveList> myLiveList);
        void onItemClick(View view, int position, List<CaiWanModel.DataBean> mList, List<CaiWanModel.DataBean.DetailsBean> mNewList);
    }

    public void setOnItemClickListener(OnItemClickListener onItemClickListener) {
        this.mOnItemClickListener = onItemClickListener;
    }

    @Override
    public PhotoViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        LayoutInflater inflater = LayoutInflater.from(parent.getContext());
        return new PhotoViewHolder(inflater.inflate(R.layout.item_caigouwan, parent, false));
    }

    @Override
    public void onBindViewHolder(PhotoViewHolder holder, int position) {
//        holder.photoView.setImageResource(photos[position]);


//        second = myList.get(position).getDetails();
       /* for (int i = 0; i < 5; i++) {
            second.add("ITem" + i);
        }*/
        second = dataBeanList.get(position).getDetails();

        if (holder.chuShiRightAdapter == null) {
            holder.chuShiRightAdapter = new CaiGouWanRightAdapter(context, second, position);
            LinearLayoutManager layoutManage = new LinearLayoutManager(context);
            holder.rv_item.setLayoutManager(layoutManage);
//            holder.rvItemItem.addItemDecoration(new GridSpacingItemDecoration(2, 20, false));
            holder.rv_item.setAdapter(holder.chuShiRightAdapter);
        } else {
            holder.chuShiRightAdapter.setPosition(position);
            holder.chuShiRightAdapter.notifyDataSetChanged();
        }

        holder.applyTime.setText(dataBeanList.get(position).getSupplierName());
        holder.chuShiRightAdapter.setOnitemClickLintener(new CaiGouWanRightAdapter.OnitemClick() {
            @Override
            public void onItemClick(View view, int position, List<CaiWanModel.DataBean.DetailsBean> mList) {
            /*    if (mOnItemClickListener != null) {
                    mOnItemClickListener.onItemClick(view, position, dataBeanList, mList);
                }*/
                switch (view.getId()) {
                    case R.id.layout_jump:
                        mOnItemClickListener.onItemClick(view, position, dataBeanList, mList);
                        break;
                }

            }
        });


        holder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (mOnItemClickListener != null) {
                    mOnItemClickListener.onItemClick(v, position, dataBeanList, second);
                }
            }
        });

        holder.rv_item.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (mOnItemClickListener != null) {
                    mOnItemClickListener.onItemClick(v, position, dataBeanList, second);
                }
            }
        });
        String evalFlag = dataBeanList.get(position).getEvalFlag();
        if (evalFlag.equals("0")) {
            holder.text_name.setText("去评价");
        } else if (evalFlag.equals("1")) {
            holder.text_name.setText("追评");
        }

        holder.text_name.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mOnItemClickListener.onItemClick(v, position, dataBeanList, second);
            }
        });


        holder.applyId.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mOnItemClickListener.onItemClick(v, position, dataBeanList, second);
            }
        });
    if (dataBeanList.get(position).getStatus().equals("1")) {
            holder.status.setText("已完成");
        } else if (dataBeanList.get(position).getStatus().equals("0")) {
            holder.status.setText("未完成");
        }

        holder.applyId.setText(dataBeanList.get(position).getBillId());

        Date now = new Date();
// 把Date按照格式转换成字符串
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        System.out.println(sdf.format(now));
// 把字符串按照格式转换成Date对象
        try {
//            Date date1 = sdf.parse(dataBean.getOrderDate());
            Date date1 = sdf.parse(dataBeanList.get(position).getBillDate());
            String date = sdf.format(date1);//格式化数据bai
//            holder.orderDate.setText(date);
            holder.applyTime.setText(date);
        } catch (ParseException e) {
            e.printStackTrace();
        }

        holder.total.setText(dataBeanList.get(position).getTotal() + "元");
    }

    @Override
    public int getItemCount() {
        return dataBeanList.size();
    }

    static final class PhotoViewHolder extends RecyclerView.ViewHolder {


        TextView text_name;
        RecyclerView rv_item;
        CaiGouWanRightAdapter chuShiRightAdapter;
        TextView status;
        TextView applyId;
        TextView applyTime;
        TextView total;

        public PhotoViewHolder(View itemView) {
            super(itemView);

            text_name = itemView.findViewById(R.id.text_submit);
            rv_item = itemView.findViewById(R.id.rv_item);
            status = itemView.findViewById(R.id.status);
            applyId = itemView.findViewById(R.id.applyId);
            applyTime = itemView.findViewById(R.id.applyTime);
            total = itemView.findViewById(R.id.price_totla);
        }
    }
}
